package com.puhui.education.view;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ListView;

import com.puhui.education.R;
import com.puhui.education.activity.MainAcy;
import com.puhui.education.adapter.ViewHolder;
import com.puhui.education.adapter.base.abslistview.CommonAdapter;
import com.puhui.education.util.TextUtil;
import com.zhy.android.percent.support.PercentFrameLayout;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/6/17.
 */
public class DropDownMenu extends PercentLinearLayout {
    @Bind(R.id.tvp_grade_dropDownMenu)
    TextViewPlus tvp_grade;
    @Bind(R.id.tvp_subject_dropDownMenu)
    TextViewPlus tvp_subject;
    @Bind(R.id.tvp_version_dropDownMenu)
    TextViewPlus tvp_version;
    @Bind(R.id.view_maskBg_dropDownMenu)
    View view_maskBg;
    @Bind(R.id.ll_container_dropDownMenu)
    PercentLinearLayout ll_container;
    @Bind(R.id.lv_menu_dropDownMenu)
    ListView lv_menu;
    @Bind(R.id.pfl_grade_dropDownMenu)
    PercentFrameLayout pfl_grade;
    @Bind(R.id.pfl_subject_dropDownMenu)
    PercentFrameLayout pfl_subject;
    @Bind(R.id.pfl_version_dropDownMenu)
    PercentFrameLayout pfl_version;
    private Context context;
    private int panelHeight = -1; //菜单高度
    private boolean isMenuShowing = false;
    private ObjectAnimator menuDropAnimator = null;
    private ObjectAnimator menuPullAnimator = null;
    private int currentShowingMenu = -1; //当前显示的菜单的position
    //各个下拉菜单的内容
    private List<String> gradeList;
    private List<String> subjectList;
    private List<String> versionList;
    private List<String> menuContentList;
    //适配器
    private MenuListViewAdapter adapter;
    //当前MenuList所包含的集合
    private int currentMenuList = -1;
    //各个集合当前选中的Item位置
    private int gradeCurrentSelectItemPosition = 0;
    private int subjectCurrentSelectItemPosition = 0;
    private int versionCurrentSelectItemPosition = 0;
    //用户选中的内容
    private String gradeSelectItem="不限";
    private String subjectSelectItem="不限";
    private String versionSelectItem="不限";
    public DropDownMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initDropDownMenu();
        initMenuListView();
    }

    private void initDropDownMenu() {
        View view = LayoutInflater.from(context).inflate(R.layout.cv_drop_down_menu, this);
        ButterKnife.bind(view);
        view_maskBg.setOnTouchListener(new OnTouchListener() { //触摸到透明区域隐藏菜单
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    switch (currentShowingMenu) {
                        case 0:
                            tvp_grade.setSelected(false);
                            break;
                        case 1:
                            tvp_subject.setSelected(false);
                            break;
                        case 2:
                            tvp_version.setSelected(false);
                            break;
                    }
                    hiddenMenu();
                    currentMenuList = -1;
                }
                return true;
            }
        });
    }

    //MenuItem点击事件
    @OnClick({R.id.pfl_grade_dropDownMenu, R.id.pfl_subject_dropDownMenu, R.id.pfl_version_dropDownMenu})
    public void onMenuItemClick(View view) {
        switch (view.getId()) {
            case R.id.pfl_grade_dropDownMenu:
                if (isMenuShowing) {
                    if (currentShowingMenu == 0) {
                        hiddenMenu();
                        tvp_grade.setSelected(false);
                        currentShowingMenu = -1;
                    } else {
                        if (!TextUtil.isValidate(gradeList)) {
                            initGradeList();
                        }
                        menuContentList.clear();
                        menuContentList.addAll(gradeList);
                        adapter.notifyDataSetChanged();
                        setMenuItemSelected(true, false, false);
                        currentShowingMenu = 0;
                        currentMenuList = 0;
                    }
                } else {
                    if (!TextUtil.isValidate(gradeList)) {
                        initGradeList();
                    }
                    if (currentMenuList != 0) {
                        menuContentList.clear();
                        menuContentList.addAll(gradeList);
                        adapter.notifyDataSetChanged();
                    }
                    showMenu();
                    tvp_grade.setSelected(true);
                    currentShowingMenu = 0;
                    currentMenuList = 0;
                }
                break;
            case R.id.pfl_subject_dropDownMenu:
                if (isMenuShowing) {
                    if (currentShowingMenu == 1) {
                        hiddenMenu();
                        tvp_subject.setSelected(false);
                        currentShowingMenu = -1;
                    } else {
                        if (!TextUtil.isValidate(subjectList)) {
                            initSubjectList();
                        }
                        menuContentList.clear();
                        menuContentList.addAll(subjectList);
                        adapter.notifyDataSetChanged();
                        setMenuItemSelected(false, true, false);
                        currentShowingMenu = 1;
                        currentMenuList = 1;
                    }
                } else {
                    if (!TextUtil.isValidate(subjectList)) {
                        initSubjectList();
                    }
                    if (currentMenuList != 1) {
                        menuContentList.clear();
                        menuContentList.addAll(subjectList);
                        adapter.notifyDataSetChanged();
                    }
                    showMenu();
                    tvp_subject.setSelected(true);
                    currentShowingMenu = 1;
                    currentMenuList = 1;
                }
                break;
            case R.id.pfl_version_dropDownMenu:
                if (isMenuShowing) {
                    if (currentShowingMenu == 2) {
                        hiddenMenu();
                        tvp_version.setSelected(false);
                        currentShowingMenu = -1;
                    } else {
                        if (!TextUtil.isValidate(versionList)) {
                            initVersionList();
                        }
                        menuContentList.clear();
                        menuContentList.addAll(versionList);
                        adapter.notifyDataSetChanged();
                        setMenuItemSelected(false, false, true);
                        currentShowingMenu = 2;
                        currentMenuList = 2;
                    }
                } else {
                    if (!TextUtil.isValidate(versionList)) {
                        initVersionList();
                    }
                    if (currentMenuList != 2) {
                        menuContentList.clear();
                        menuContentList.addAll(versionList);
                        adapter.notifyDataSetChanged();
                    }
                    showMenu();
                    tvp_version.setSelected(true);
                    currentShowingMenu = 2;
                    currentMenuList = 2;
                }
                break;
        }
    }

    private void initGradeList() {
        gradeList = new ArrayList<>();
        gradeList.add("不限s");
        gradeList.add("初一");
        gradeList.add("初二");
        gradeList.add("初三");
        gradeList.add("高一");
        gradeList.add("高二");
        gradeList.add("高三");
    }

    private void initSubjectList() {
        subjectList = new ArrayList<>();
        subjectList.add("不限s");
        subjectList.add("语文");
        subjectList.add("数学");
        subjectList.add("英语");
        subjectList.add("物理");
        subjectList.add("化学");
        subjectList.add("生物");
    }

    private void initVersionList() {
        versionList = new ArrayList<>();
        versionList.add("不限s");
        versionList.add("人教版");
        versionList.add("北师版");
        versionList.add("沪教版");
    }

    //初始化ListView
    private void initMenuListView() {
        menuContentList = new ArrayList<>();
        adapter = new MenuListViewAdapter(context, R.layout.item_lv_drop_down_menu, menuContentList);
        lv_menu.setAdapter(adapter);
        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //最开始默认是选中“不限”
                String clickItem = menuContentList.get(position);
                switch (currentMenuList) {
                    case 0:
                        if (gradeCurrentSelectItemPosition != position) {
                            gradeSelectItem = clickItem.replace("s", "");
                            menuContentList.set(gradeCurrentSelectItemPosition, menuContentList.get(gradeCurrentSelectItemPosition).replace("s", ""));
                            menuContentList.set(position, clickItem + "s");
                            if (clickItem.contains("不限")) {
                                tvp_grade.setText("年级");
                            } else {
                                tvp_grade.setText(clickItem.replace("s", ""));
                            }
                            gradeList.set(gradeCurrentSelectItemPosition, gradeList.get(gradeCurrentSelectItemPosition).replace("s", ""));
                            gradeList.set(position, clickItem + "s");
                        }
                        gradeCurrentSelectItemPosition = position;
                        tvp_grade.setSelected(false);
                        break;
                    case 1:
                        if (subjectCurrentSelectItemPosition != position) {
                            subjectSelectItem=clickItem.replace("s", "");
                            menuContentList.set(subjectCurrentSelectItemPosition, menuContentList.get(subjectCurrentSelectItemPosition).replace("s", ""));
                            menuContentList.set(position, clickItem + "s");
                            if (clickItem.contains("不限")) {
                                tvp_subject.setText("科目");
                            } else {
                                tvp_subject.setText(clickItem.replace("s", ""));
                            }
                            subjectList.set(subjectCurrentSelectItemPosition, subjectList.get(subjectCurrentSelectItemPosition).replace("s", ""));
                            subjectList.set(position, clickItem + "s");
                        }
                        subjectCurrentSelectItemPosition = position;
                        tvp_subject.setSelected(false);
                        break;
                    case 2:
                        if (versionCurrentSelectItemPosition != position) {
                            versionSelectItem=clickItem.replace("s", "");
                            menuContentList.set(versionCurrentSelectItemPosition, menuContentList.get(versionCurrentSelectItemPosition).replace("s", ""));
                            menuContentList.set(position, clickItem + "s");
                            if (clickItem.contains("不限")) {
                                tvp_version.setText("版本");
                            } else {
                                tvp_version.setText(clickItem.replace("s", ""));
                            }
                            versionList.set(versionCurrentSelectItemPosition, versionList.get(versionCurrentSelectItemPosition).replace("s", ""));
                            versionList.set(position, clickItem + "s");
                        }
                        versionCurrentSelectItemPosition = position;
                        tvp_version.setSelected(false);
                        break;
                }
                adapter.notifyDataSetChanged();
                hiddenMenu();
                currentShowingMenu = -1;
                //选择结束后将结果传递到相应的Activity中
                ((MainAcy)context).onMenuItemSelectCompleted(gradeSelectItem,subjectSelectItem,versionSelectItem);
            }
        });
    }

    //弹出菜单
    private void showMenu() {
        view_maskBg.setVisibility(VISIBLE);
        ll_container.setVisibility(VISIBLE);
        ll_container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ll_container.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                panelHeight = ll_container.getHeight();
                if (menuDropAnimator == null) {
                    menuDropAnimator = new ObjectAnimator().ofFloat(ll_container, "translationY", -panelHeight, 0).setDuration(250);
                    menuDropAnimator.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {
                            setMenuItemClickable(false);
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            isMenuShowing = true;
                            setMenuItemClickable(true);
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                }
                menuDropAnimator.start();
            }
        });

    }

    //隐藏菜单
    private void hiddenMenu() {
        view_maskBg.setVisibility(View.GONE);
        if (menuPullAnimator == null) {
            menuPullAnimator = new ObjectAnimator().ofFloat(ll_container, "translationY", 0, -panelHeight).setDuration(250);
            menuPullAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    setMenuItemClickable(false);
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    isMenuShowing = false;
                    setMenuItemClickable(true);
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        }
        menuPullAnimator.start();
    }

    //设置菜单项是否可以点击
    private void setMenuItemClickable(boolean isClickable) {
        pfl_grade.setClickable(isClickable);
        pfl_subject.setClickable(isClickable);
        pfl_version.setClickable(isClickable);
    }

    //设置菜单项是否选中
    private void setMenuItemSelected(boolean grade, boolean subject, boolean version) {
        tvp_grade.setSelected(grade);
        tvp_subject.setSelected(subject);
        tvp_version.setSelected(version);
    }
    //回调接口,用于选择完成时选中结果的传递
    public interface Filter {
        void onMenuItemSelectCompleted(String grade,String subject,String version);
    }
}

class MenuListViewAdapter extends CommonAdapter<String> {

    public MenuListViewAdapter(Context context, int layoutId, List datas) {
        super(context, layoutId, datas);
    }


    @Override
    public void convert(ViewHolder holder, String s) {
        holder.setText(R.id.tv_title_itemLvDropDownMenu, s.replace("s", ""));
        if (s.contains("s")) {
            holder.getView(R.id.tv_title_itemLvDropDownMenu).setSelected(true);
            holder.getView(R.id.tv_right_itemLvDropDownMenu).setVisibility(View.VISIBLE);
            holder.getView(R.id.tv_right_itemLvDropDownMenu).setSelected(true);
        } else {
            holder.getView(R.id.tv_title_itemLvDropDownMenu).setSelected(false);
            holder.getView(R.id.tv_right_itemLvDropDownMenu).setVisibility(View.GONE);
            holder.getView(R.id.tv_right_itemLvDropDownMenu).setSelected(false);
        }
    }
}
